﻿function CookieHelper() {

}
//删除cookie
CookieHelper.prototype.deleteCookie = function (name) {
    var date = new Date();
    date.setTime(date.getTime() - 10000);
    document.cookie = name + "=v; expires=" + date.toGMTString() + ";path=/";
}



//设置cookie
CookieHelper.prototype.setCookie = function (name, value) {
    //设置之前先删除
    this.deleteCookie(name);
    //获取当前时间 
    var date = new Date();
    var expiresDays = 10;
    //将date设置为10天以后的时间 
    date.setTime(date.getTime() + expiresDays * 24 * 3600 * 1000);
    //cookie设置为10天后过期 
    document.cookie = name + "=" + value + "; expires=" + date.toGMTString() + ";path=/";
    document.cookie
}

//获取某一cookie key=value
CookieHelper.prototype.getCookie = function (name)
{
    try {
        var strCookie = document.cookie;
        var arrCookie = strCookie.split("; ");
        for (var i = 0; i < arrCookie.length; i++) {
            var arr = arrCookie[i].split("=");
            if (arr[0] == name) {
                return arr[1];
            }
        }
    } catch (Exception)
    {
        return "";
    }
    return "";
}
//获取cookie 类型 key=id=123&name=qwe&pwd=uio
CookieHelper.prototype.getCookies = function (name) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; ");
    for (var i = 0; i < arrCookie.length; i++) {
        var arr = arrCookie[i].split("=");
        if (arr[0] == name) {
            return arrCookie[i].substring(arrCookie[i].indexOf("=") + 1)
        }
    }
    return "";
}


CookieHelper.prototype.getCookies2 = function (name,Key) {

    var _topCookiesValue = this.getCookies(name);
    for (var i = 0; i < _topCookiesValue.split('&').length; i++)
    {
        if (_topCookiesValue.split('&')[i].split('=')[0] == Key) {
            return _topCookiesValue.split('&')[i].split('=')[1];
        }
    };
    return "";
}

//获取所有cookie

CookieHelper.prototype.getAllCookie = function (name) {
    var strCookie = document.cookie;
    var arrCookie = strCookie.split("; ");
    if (arrcookie.length > 0) {
        return arrCookie;
    } else {
        return "";
    }
}

var cookiehelp = new CookieHelper();